package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.system.mapper.DepositLogMapper;

import com.ruoyi.system.mapper.HatchetMerchantIncreaseLimitMapper;
import com.ruoyi.system.domain.DepositLog;
/*import com.ruoyi.system.mapper.HatchetIssBankMapper;
import com.ruoyi.system.domain.HatchetIssBank;
import com.ruoyi.system.domain.HatchetMerchant;
import com.chuanglan.SMSConst;
import com.chuanglan.sms.util.ChuangLanSmsUtil;
import com.ruoyi.common.utils.Constans;
import com.ruoyi.system.domain.OptLog;
import com.ruoyi.system.service.IHatchetMerchantService;
import com.ruoyi.system.service.IOptLogService;
import com.ruoyi.system.service.IHatchetIssBankService;
*/
import com.ruoyi.system.domain.HatchetMerchantIncreaseLimit;
import com.ruoyi.system.domain.HatchetPlan;
import com.ruoyi.system.service.IHatchetMerchantIncreaseLimitService;


import com.ruoyi.common.enums.BusinessStatus;
import com.ruoyi.common.support.Convert;

import com.ruoyi.common.utils.StringUtils;

/**
 * 商户信用卡 服务层实现
 * 
 * @author ruoyi
 * @date 2019-03-02
 */
@Service
public class HatchetMerchantIncreaseLimitServiceImpl implements IHatchetMerchantIncreaseLimitService 
{
	@Autowired
	private HatchetMerchantIncreaseLimitMapper hatchetMerchantIncreaseLimitMapper;
	@Autowired
	private DepositLogMapper  depositLogMapper;
/*	@Autowired
	private IHatchetIssBankService issBankService;
	@Autowired
	private IHatchetMerchantService merchantService;
	@Autowired
	private IOptLogService optLogService;
*/
	/**
     * 查询商户信用卡信息
     * 
     * @param iD 商户信用卡ID
     * @return 商户信用卡信息
     */
    @Override
	public HatchetMerchantIncreaseLimit selectHatchetMerchantIncreaseLimitById(String iD)
	{
	    return hatchetMerchantIncreaseLimitMapper.selectHatchetMerchantIncreaseLimitById(iD);
	}
	
	/**
     * 查询商户信用卡列表
     * 
     * @param hatchetMerchantIncreaseLimit 商户信用卡信息
     * @return 商户信用卡集合
     */
	@Override
	public List<HatchetMerchantIncreaseLimit> selectHatchetMerchantIncreaseLimitList(HatchetMerchantIncreaseLimit hatchetMerchantIncreaseLimit)
	{
	    return hatchetMerchantIncreaseLimitMapper.selectHatchetMerchantIncreaseLimitList(hatchetMerchantIncreaseLimit);
	}
	
    /**
     * 新增商户信用卡
     * 
     * @param hatchetMerchantIncreaseLimit 商户信用卡信息
     * @return 结果
     */
	@Override
	public int insertHatchetMerchantIncreaseLimit(HatchetMerchantIncreaseLimit hatchetMerchantIncreaseLimit)
	{
	    return hatchetMerchantIncreaseLimitMapper.insertHatchetMerchantIncreaseLimit(hatchetMerchantIncreaseLimit);
	}
	
	/**
     * 修改商户信用卡
     * 
     * @param hatchetMerchantIncreaseLimit 商户信用卡信息
     * @return 结果
     */
	@Override
	public int updateHatchetMerchantIncreaseLimit(HatchetMerchantIncreaseLimit hatchetMerchantIncreaseLimit)
	{
	    return hatchetMerchantIncreaseLimitMapper.updateHatchetMerchantIncreaseLimit(hatchetMerchantIncreaseLimit);
	}
	

	/**
     * 删除商户信用卡对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteHatchetMerchantIncreaseLimitByIds(String ids)
	{
		return hatchetMerchantIncreaseLimitMapper.deleteHatchetMerchantIncreaseLimitByIds(Convert.toStrArray(ids));
	}

	/**
	 * 通过银行卡号查询信用卡信息
	 */
	public HatchetMerchantIncreaseLimit selectHatchetMerchantIncreaseLimitByCardNo(String cardNo) {
		HatchetMerchantIncreaseLimit  increaseLimit = new HatchetMerchantIncreaseLimit();
		increaseLimit.setBankAccount(cardNo);
		increaseLimit.setIncreaseLimitStatus("10B");
		List<HatchetMerchantIncreaseLimit> increaseList = selectHatchetMerchantIncreaseLimitList(increaseLimit);
		if (StringUtils.isNotEmpty(increaseList)) {
			return increaseList.get(0);
		}
		return null;
	}

	/**
	 * 经理人查询信用卡信息 不查敏感安全字段
	 * @param hatchetMerchantIncreaseLimit
	 * @return
	 */
	public List<HatchetMerchantIncreaseLimit> selectHatchetMerchantIncreaseLimitListForJLR(
			HatchetMerchantIncreaseLimit hatchetMerchantIncreaseLimit) {
		
		return hatchetMerchantIncreaseLimitMapper.selectHatchetMerchantIncreaseLimitListForJLR(hatchetMerchantIncreaseLimit);
	}

	/**
	 * 查看信用卡在跑的计划(计划表的sTATUS:10A未执行，10B进行中)
	 * @param limit
	 * @return
	 */
	public List<HatchetPlan> selectPlanByLimit(HatchetMerchantIncreaseLimit limit) {
		return	hatchetMerchantIncreaseLimitMapper.selectPlanByLimit(limit);
		
	}

	/**
	 * 处理委托
	 * @param hatchetMerchantIncreaseLimit
	 * @return
	 */
	@Transactional
	public Map<String, Object> doEntrust(HatchetMerchantIncreaseLimit hatchetMerchantIncreaseLimit) {
		Map<String, Object> map = new HashMap<>();
		HatchetMerchantIncreaseLimit increaseLimit = selectHatchetMerchantIncreaseLimitById(hatchetMerchantIncreaseLimit.getID());
		//String merchantId = increaseLimit.getMerchantId();
		//HatchetMerchant merchant = merchantService.selectHatchetMerchantById(merchantId);
		if (StringUtils.isNotEmpty(increaseLimit.getAuditRecord())) {
			String auditRecord = increaseLimit.getAuditRecord();
			String[] split = auditRecord.split(":");
			String entrustStatus = hatchetMerchantIncreaseLimit.getEntrustStatus();
			String brokerid = split[1];//高级经理人id
			increaseLimit.setAuditRecord(entrustStatus+":"+brokerid);
			int update = updateHatchetMerchantIncreaseLimit(increaseLimit);
			if (update>0) {
				//添加一条信用卡委托记录
				DepositLog depositLog = new DepositLog();
				depositLog.setLimitid(increaseLimit.getID());
				depositLog.setBrokerid(brokerid);
				depositLog.setStatus(entrustStatus);
				depositLog.setUid(brokerid);
				depositLog.setTime(new Date());
				depositLog.setType("2");//高级经纪人处理委托
				int insertDepositLog = depositLogMapper.insertDepositLog(depositLog);
				if (insertDepositLog>0) {
					/*HatchetMerchant jlr = merchantService.selectHatchetMerchantById(brokerid);
					HatchetIssBank issBank = issBankService.selectHatchetIssBankByBankCode(increaseLimit.getBankName());
					String params ="";//短信内容
					String type ="";//短信类型
*/					//如果拒绝委托 清除经理人信息字段
					if ("10C".equals(entrustStatus)) {
						//type="deposit_mer_apply_defeated_sms";//类型 ：经理不人接受
						int clearAuditRecord = hatchetMerchantIncreaseLimitMapper.clearAuditRecord(increaseLimit.getID());
						if (clearAuditRecord>0) {
							//给用户发短信
							//13026186535,中国银行(5657)托管申请被高级经纪人拒绝
							//13026186535,民生银行(4895)托管申请被高级经纪人拒绝
						/*	params = merchant.getPHONE()+","+issBank.getShortCnName()+"("+increaseLimit.getBankAccount().substring(increaseLimit.getBankAccount().length()-4)+")"
									 		+"托管申请被高级经纪人拒绝";
							String eee = ChuangLanSmsUtil.sendSMSVar(SMSConst.deposit_mer_apply_defeated, params);
							System.out.println(eee);*/
							map.put("status", BusinessStatus.SUCCESS);
							map.put("msg", "操作成功");
						}else {
							throw new RuntimeException("操作失败");
						}
					}else {
						//type="deposit_mer_apply_success_sms";//类型 ：经理人接受
						//接受委托
						//13026186535,光大银行(4233),高级经纪人谢学琴13429978617受理，受理,高级经纪人谢学琴,高级经纪人,高级经纪人	
						//13927459802,中国银行(5657),高级经纪人杨振伟18676677004受理，受理,高级经纪人杨振伟,高级经纪人,高级经纪人
						//【卡时代】尊敬的卡时代会员，您的信用卡{$var}已成功被{$var}期间请您与{$var}保持联系，在账单日及时告知{$var}月还款金额和卡内可用额度，{$var}会帮您制定还款计划，感谢您的信任与支持，卡时代一直陪在您身边！"; 
 					/*	params = merchant.getPHONE()+","+issBank.getShortCnName()+"("+increaseLimit.getBankAccount().substring(increaseLimit.getBankAccount().length()-4)+")"
						 		+","+SMSConst.JLR_TITLE+jlr.getMerchantCnName()+jlr.getPHONE()+"受理，受理,"+SMSConst.JLR_TITLE+jlr.getMerchantCnName()+","+SMSConst.JLR_TITLE+","+SMSConst.JLR_TITLE;
						//发送短信
						String eee = ChuangLanSmsUtil.sendSMSVar(SMSConst.deposit_mer_apply_success, params);
						System.out.println(eee);*/
						map.put("status", BusinessStatus.SUCCESS);
						map.put("msg", "操作成功");
					}
					//创建一条短信日志记录
				/*	OptLog optLog = new OptLog();
					optLog.setUid(increaseLimit.getMerchantId());
					optLog.setPhone(merchant.getPHONE());
					optLog.setSend(params);
					optLog.setReturnMsg("success");
					optLog.setType(type);
					optLog.setAcq(Constans.SYS_ACQ);
					optLog.setCreateTime(new Date());
					optLogService.insertOptLog(optLog);*/
					
				} else {
					throw new RuntimeException("操作失败");
				}
			}else {
				map.put("status", BusinessStatus.FAIL);
				map.put("msg", "操作失败");
			}
		}else {
			map.put("status", BusinessStatus.FAIL);
			map.put("msg", "操作失败");
		}
		return map;
	}
	
}
